package com.yyfax.pay.quartz.adapter.impl;

import com.yyfax.framework.common.Log;
import com.yyfax.pay.common.annotation.QueryTaskType;
import com.yyfax.pay.common.enums.QueryTaskTypeEnum;
import com.yyfax.pay.depository.domain.FuiouQueryTask;
import com.yyfax.pay.order.data.service.ProtocolPayService;
import com.yyfax.pay.order.model.po.QueryTaskPO;
import com.yyfax.pay.quartz.adapter.DepositQueryHandlerAdapter;
import com.yyfax.pay.quartz.adapter.QueryHandlerAdapter;
import com.yyfax.pay.quartz.model.vo.TaskResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("fuiouProtocolPayQueryHandler")
@QueryTaskType(QueryTaskTypeEnum.PROTOCOL_PAY)
public class FuiouProtocolPayQueryHandler implements QueryHandlerAdapter {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private ProtocolPayService protocolPayService;

    @Override
    public TaskResultVO handle(QueryTaskPO task) throws Exception {
        final String OP = "FuiouPaotocolPayQueryHandler.handle";
        logger.info(Log.op(OP).msg("开始执行协议支付超时查询任务").kv("task", task).toString());
        boolean isResult = protocolPayService.queryResult(task.getTradeOrderId());

        if (isResult) {
            return new TaskResultVO(true);
        }

        if (task.getOptNo() >= task.getMaxNo()) {
            logger.error(Log.op(OP).msg("协议卡查询达到最大次数，订单还未出结果。").kv("busiId", task.getTradeOrderId()).toString());
        }
        return new TaskResultVO(false);
    }
}
